const express = require('express');
const router = express.Router();
const BudgetController = require('../controllers/BudgetController');
const authenticate = require('../middleware/auth');

/**
 * @swagger
 * tags:
 *   name: Budgets
 *   description: 预算管理
 */

// All routes require authentication
router.use(authenticate);

/**
 * @swagger
 * /api/budgets:
 *   post:
 *     summary: 创建新预算
 *     tags: [Budgets]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - amount
 *               - period
 *               - start_date
 *             properties:
 *               category_id:
 *                 type: integer
 *                 description: 分类ID（可选，全局预算则不填）
 *               amount:
 *                 type: number
 *                 format: float
 *                 description: 预算金额
 *               period:
 *                 type: string
 *                 enum: [daily, weekly, monthly, yearly]
 *                 description: 预算周期
 *               start_date:
 *                 type: string
 *                 format: date
 *                 description: 开始日期 (YYYY-MM-DD)
 *               end_date:
 *                 type: string
 *                 format: date
 *                 description: 结束日期 (YYYY-MM-DD)
 *     responses:
 *       201:
 *         description: 预算创建成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.post('/', BudgetController.createBudget);

/**
 * @swagger
 * /api/budgets:
 *   get:
 *     summary: 获取用户预算列表
 *     tags: [Budgets]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取预算列表
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/', BudgetController.getBudgets);

/**
 * @swagger
 * /api/budgets/status:
 *   get:
 *     summary: 检查预算状态
 *     tags: [Budgets]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: date
 *         schema:
 *           type: string
 *           format: date
 *         description: 日期 (YYYY-MM-DD)
 *     responses:
 *       200:
 *         description: 成功获取预算状态
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/status', BudgetController.getBudgetStatus);

/**
 * @swagger
 * /api/budgets/{id}:
 *   get:
 *     summary: 获取特定预算详情
 *     tags: [Budgets]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 预算ID
 *     responses:
 *       200:
 *         description: 成功获取预算详情
 *       401:
 *         description: 未授权
 *       404:
 *         description: 预算未找到
 *       500:
 *         description: 服务器内部错误
 */
router.get('/:id', BudgetController.getBudgetById);

/**
 * @swagger
 * /api/budgets/{id}:
 *   put:
 *     summary: 更新预算
 *     tags: [Budgets]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 预算ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               category_id:
 *                 type: integer
 *                 description: 分类ID
 *               amount:
 *                 type: number
 *                 format: float
 *                 description: 预算金额
 *               period:
 *                 type: string
 *                 enum: [daily, weekly, monthly, yearly]
 *                 description: 预算周期
 *               start_date:
 *                 type: string
 *                 format: date
 *                 description: 开始日期 (YYYY-MM-DD)
 *               end_date:
 *                 type: string
 *                 format: date
 *                 description: 结束日期 (YYYY-MM-DD)
 *     responses:
 *       200:
 *         description: 预算更新成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 预算未找到
 *       500:
 *         description: 服务器内部错误
 */
router.put('/:id', BudgetController.updateBudget);

/**
 * @swagger
 * /api/budgets/{id}:
 *   delete:
 *     summary: 删除预算
 *     tags: [Budgets]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 预算ID
 *     responses:
 *       200:
 *         description: 预算删除成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 预算未找到
 *       500:
 *         description: 服务器内部错误
 */
router.delete('/:id', BudgetController.deleteBudget);

module.exports = router;